// mircoApp/groupPurchase/purchaseIndex/purchaseIndex.js
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    value:"",
    btnType:2,
    purchaseTab:1,
    timeData: {},
    imgPath: app.globalData.imgPath,
    ferventImg:`${app.globalData.imgPath}yz-group-buy-activity/fervent.png`,
    ferventGray:`${app.globalData.imgPath}yz-group-buy-activity/fervent-grey.png`,
    imageText:`${app.globalData.imgPath}yz-group-buy-activity/purchase.png`,
    imggray:`${app.globalData.imgPath}yz-group-buy-activity/purchase-grey.png`,
    remindImg:`${app.globalData.imgPath}yz-group-buy-activity/remind.png`,
    remidText:`${app.globalData.imgPath}yz-group-buy-activity/remind-grey.png`,
    
    tabData:[
      {
        name:'正在抢购',
        value:1,
      },
      {
        name:'团购预告',
        value:2,
      }
    ],
    page:1,
    isLoadMore: true,
    purLoading:true,
    totalPage: 0,
    purchaseList:[
    ],
    timer:"",
    purchaseTime:'',
    presentTime:'',
    timeStart:'', //预告日期字段
    style: {  
      // 定义CSS变量  
      fervent: '',
      ferventRight:'',
      purchaseBg:''
    },
    ferventBg:'',
    actBg:''  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
      this.data.presentTime = new Date().getTime();
      this.styleFun();
      this.actIndex();
      
  },
  styleFun() {
    this.setData({
      ['style.purchaseBg']:`background-image:url("${this.data.imgPath}yz-group-buy-activity/group-bg.png");`,
      ['style.fervent']:`background:url('${this.data.imgPath}yz-group-buy-activity/group-left.png') no-repeat top;background-size: contain;`,
      ['style.ferventRight']:`background:url('${this.data.imgPath}yz-group-buy-activity/group-right.png') no-repeat top;background-position: top;background-size: contain;`
    })
  },
  tabClick(event) {
    let {value} = event.currentTarget.dataset;
    if(this.data.purchaseTab == value) return
    console.log(value); 
    this.setData({
      purchaseTab:value,
      purchaseList:[],
      timeStart:'',
      isLoadMore:true,
      purLoading:true,
      page:1
    });
    console.log(this.data.purchaseTab);
    this.actIndex()
  },
  //返回上一页
  onBack() {
    const pages = getCurrentPages();
    if(pages.length === 1) {
      wx.navigateTo({ url:'/packageG/index/index'})										
    } else {
      wx.navigateBack({ delta: 1 })
    }
  },
  //跳转详情
  goodsTo(event) {
    let item = event.currentTarget.dataset.item;
    wx.navigateTo({
      url: `/mircoApp/groupPurchase/purchaseDetails/purchaseDetails?id=${item.has_one_good?.id}`
    });
  },
  actIndex() {
    wx.showLoading({
      title: '加载中',
      mask:true
    })
    let url = app.getNetAddresss("plugin.yz-group-buy-activity.api.index.index");
    url += `&search[state]=${this.data.purchaseTab}&page=${this.data.page}&basic_info=1`;
    this.data.purLoading = false;
    app._getNetWork({
      url,
      success: (resdata) => {
        let res = resdata.data;
        let data = res.data;
        let list = res.data.data;
        wx.hideLoading();
        wx.hideNavigationBarLoading();//隐藏导航条加载动画
        //停止下拉刷新
        wx.stopPullDownRefresh();
        if (res.result) {
          this.data.totalPage = data.last_page;
          if (!this.data.totalPage) {
            this.data.totalPage = 0;
          }
          let actTime = ''; 
          if(this.data.purchaseTab == 1) {
            actTime = (list[0]?.time_end * 1000) - this.data.presentTime;
          } else {
            this.data.timeStart = list[0]?.time_start_str?.split(" ")[0] || '';
            actTime =  (list[0]?.time_start * 1000) - this.data.presentTime;
          }
          this.setData({
            purchaseList:list,
            timeStart:this.data.timeStart,
            purchaseTime:actTime
          })
        }
        wx.hideLoading();
        this.setData({
          purLoading:false
        })
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  purChange(event) {
    this.setData({
      timeData: event.detail,
    });
  },
  //获取更多数据
  getMoreData() {
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.totalPage) {
      return;
    } else {
      this.data.page = this.data.page + 1;
      let urlStr = app.getNetAddresss("plugin.yz-group-buy-activity.api.index.index");
      urlStr += `&search[state]=${this.data.purchaseTab}&page=${this.data.page}`;
      app._getNetWork({
        url: urlStr,
        success: (resdata) => {
          let res = resdata.data;
          let list = res.data?.data;
          if (res.result == 1) {
            this.data.isLoadMore = true;
            this.setData({
              purchaseList : this.data.purchaseList.concat(list),
            })
          } else {
            this.data.page = this.data.page - 1;
            this.isLoadMore = false;
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    wx.showNavigationBarLoading();
     //显示 loading 提示框。需主动调用 wx.hideLoading 才能关闭提示框
     wx.showLoading({
      title: '刷新中...',
    })
    this.setData({
      page:1,
      purchaseList:[],
      timeStart:'',
      purLoading:true,
      isLoadMore: true,
    })
    this.actIndex();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    } else {
      console.log('没有更多了');
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})